using System;
using System.Collections.Generic;
using System.Text;

using System.Collections;

using NHibernate.Expression;
using Castle.ActiveRecord;

namespace Adge.Models
{
    [ActiveRecord("PostTable")]
    public class Post : ActiveRecordBase<Post>
    {
        private int _id;
        private String _title;
        private String _contents;
        private String _category;
        private DateTime _created;
        private bool _published;
        private Blog _blog;

        public Post()
        {
        }

        public Post(Blog blog, String title, String contents, String category)
        {
            _blog = blog;
            _title = title;
            _contents = contents;
            _category = category;
        }

        [PrimaryKey]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [Property]
        public String Title
        {
            get { return _title; }
            set { _title = value; }
        }

        [Property(ColumnType = "StringClob")]
        public String Contents
        {
            get { return _contents; }
            set { _contents = value; }
        }

        [Property]
        public String Category
        {
            get { return _category; }
            set { _category = value; }
        }

        [BelongsTo("blogid")]
        public Blog Blog
        {
            get { return _blog; }
            set { _blog = value; }
        }

        [Property("created")]
        public DateTime Created
        {
            get { return _created; }
            set { _created = value; }
        }

        [Property("published")]
        public bool Published
        {
            get { return _published; }
            set { _published = value; }
        }

        public void SaveWithException()
        {
            Save();

            throw new ApplicationException("Fake Exception");
        }

        protected override bool BeforeSave(IDictionary state)
        {
            state["Created"] = DateTime.Now;
            return true;
        }

        public static Post[] CustomSlicedFind(ICriterion criteria, int firstResult, int maxResults)
        {
            return SlicedFindAll(firstResult, maxResults, criteria);
        }

    }
}
